<?php

declare(strict_types=1);

namespace App\Job;

use App\Model\CashLog;
use App\Model\Distributors;
use App\Model\Normal;
use App\Model\Order;
use Hyperf\AsyncQueue\Job;
use Hyperf\Di\Annotation\Inject;
use App\Services\AdapayService;

class CashJob extends Job
{
    public int $distributor_id;
    public string $type;
    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     */
    protected int $maxAttempts = 2;
    #[inject]
    protected AdapayService $adapayService;

    public function __construct($distributor_id)
    {
        $this->type = $type;
        $this->distributor_id = $distributor_id;
    }

    public function handle()
    {
        $distributor = $this->distributors->where('id', $this->distributor_id)
            ->with('business')
            ->first();
        $save['balance_no'] = "TX_" . $info['id'] . date("YmdHis") . rand(1000, 9999);
        $money  = $distributor->balance;
        $params = array(
            'order_no' => $save['balance_no'] ,
            'money' => $distributor->balance,
            'acct_split_bunch' => [
                'acct_infos'=>[
                    'div_amt'=>$money,
                    'huifu_id' => $distributor->third_account_id
                ]
            ]
        );
        $result = $service->balancePay($params);
        if($distributor->type == 1){
            $save['third_account_id'] = $distributor->third_account_id;
            $save['bank_name'] = $distributor->withdraw_account_bank_name;
            $save['bank_no'] = $distributor->withdraw_account_bank_card_no;
            $save['cert_name'] = $distributor->withdraw_account_bank_card_username;

        }else{
            $save['third_account_id'] = $distributor->third_account_id;
            $save['bank_name'] = $distributor->business->reg_name;
            $save['bank_no'] = $distributor->business->card_no;
            $save['cert_name'] = $distributor->business->legal_name;

        }
        if($result['response']['data']['resp_code'] != '00000000'){
            Normal::logger($this->distributor_id.'--余额支付失败'.json_encode($result), 'notify-event');
            return;
        }
        $save['balance_hf_id'] = $result['response']['data']['hf_seq_id'];
        $save['type'] = $distributor->settlement_type;
        $save['pay_no'] = $params['order_no'];
        $save['cash_no'] = "CS_" . $info['id'] . date("YmdHis") . rand(1000, 9999);
        $save['cash_amt'] = $money;
        $save['cash_type'] = $type;
        $save['distributor_id'] = $info['id'];
        $save['balance_date'] = date('Ymd');
        $cashLog = CashLog::create($save);
        $cashParams = array(
            'order_no' => $save['cash_no'],
            'third_account_id' => $distributor->third_account_id,
            'money' => $money,
            'token_no' => $distributor->settle_account_id
        );
        $cashResult = $service->drawCash($cashParams);
        Normal::logger($this->distributor_id.'取现结果'.json_encode($cashResult), 'notify-event');
        if ($cashResult['response']['data']['resp_code'] != '00000000'){
            $cashLog->status = CashLog::COMMIT_FALSE;
            $cashLog->error_code = $cashResult['response']['data']['resp_code'];
            $cashLog->error_msg = $cashResult['response']['data']['resp_desc'];
            $cashLog->refund_no = "TXRF_" . date("YmdHis") . rand(10000, 99999);
            $refund_params = array(
                'refund_no' => $cashLog->refund_no,
                'balance_date' => $cashLog->balance_date,
                'balance_hf_id'=>$cashLog->balance_hf_id,
                'money' => $cashLog->cash_amt,
                'third_account_id' => $cashLog->third_account_id
            );
            $refundResult = $service->balanceRefund($refund_params);
            Normal::logger($this->distributor_id.'余额退款结果'.json_encode($cashResult), 'notify-event');
            if ($refundResult['response']['data']['resp_code'] != '00000000'){
                $cashLog->refund_msg = $refundResult['response']['data']['resp_desc'];
            } else {
                $cashLog->refund_msg = '回滚退款成功';
                $cashLog->is_return = 1;
            }
            $cashLog->save();
        }
        $distributor->balance = $distributor->balance - $money;
        $distributor->save();
        return '';

    }
}
